<?php
namespace Home\Model;
use Think\Model;
use Think\Vender;
class UserModel extends Model {

    public function addUser($data){
        $user_id=$this->data($data)->add();
        if(!$user_id)return 0;
        if(!D('Avatar')->addAvatar(['user_id'=>$user_id]))return 0;
        return 1;
    }

//    public function deleteAllUser(){
//        return $this->delete();
//    }

    public function deleteUserById($id){
        return $this->where("id='$id'")->delete();// 返回删除记录个数
    }

    public function updateUserBaseInfoById($id,$data){
//        可以做一些邮箱二次验证之类的
//        if(....){
//            return ['status'=>'error','info'=>'邮箱格式错误'];
//        }
        $this->where("id='$id'")->save($data);
        return ['status'=>'success','info'=>'信息基本信息更新成功'];
    }
    public function updateUserPasswordById($id,$data){
        if($data['new_passowrd']!=$data['again_new_passowrd'])
            return ['status'=>'error','info'=>'两次密码不一致'];
        $user=$this->getUserById($id);
        if($user[0]['password']==$data['old_password']){
            $this->where("id='$id'")->save(['password'=>$data['new_passowrd']]);
            return ['status'=>'success','info'=>'密码更新成功'];
        }
        return ['status'=>'error','info'=>'原密码不正确'];
    }

    public function updateUserById($id,$data){
        $this->where("id='$id'")->save($data);
    }

    public function updateUserByUsername($username,$data){
        $this->where("username='$username'")->save($data);
    }

    public function getAllUser(){
        $users= $this->select();
        foreach($users as $key => $user){
            $avatar_data=D('Avatar')->getAvatarByUserId($user['id']);
            $users[$key]['avatar_name']=$avatar_data[0]['name'];
        }
        return $users;
    }

    public function searchUserByKeyword($Keyword){
        $data['username']  = array('like',"%$Keyword%");
        $users= D('User')->where($data)->select();
        foreach($users as $key => $user){
            $avatar_data=D('Avatar')->getAvatarByUserId($user['id']);
            $users[$key]['avatar_name']=$avatar_data[0]['name'];
        }
        return $users;
    }

    public function getUserOrderByRigisterTimeDesc($number){
        return $this->limit($number)->order('register_time desc')->select();
    }

    public function getUserOrderByLoginTimeDesc($number){
        return $this->limit($number)->order('lastlogin_time desc')->select();
    }


    public function getActiveUserByNumber($number){

        $newest_user_datas=D('User')->getUserOrderByLoginTimeDesc($number);

        foreach($newest_user_datas as $key => $newest_user_data){
            $Avatar_data=D('Avatar')->getAvatarByUserId($newest_user_data['id']);
            $newest_user_datas[$key]['avatar_name']=$Avatar_data[0]['name'];
        }
        return $newest_user_datas;
    }

    public function getUserOrderByIdDesc($number){
        return $this->limit($number)->order('id desc')->select();
    }

    public function getNewestUserByNumber($number){
        $active_user_datas=D('User')->getUserOrderByIdDesc($number);
        foreach($active_user_datas as $key => $active_user_data){
            $Avatar_data=D('Avatar')->getAvatarByUserId($active_user_data['id']);
            $active_user_datas[$key]['avatar_name']=$Avatar_data[0]['name'];
        }
        return $active_user_datas;
    }



    public function getUserByArticleId($article_id){
        $article = D('Article')->where("id='$article_id'")->select();
        $id=$article[0]['author_id'];
        $user = $this->where("id='$id'")->select();
        return $user;
    }

    public function getUserById($id){
        return $this->where("id='$id'")->select();
    }

    public function getUserByUsername($username){
        return $this->where("username='$username'")->select();
    }
    public function getUserByEmail($email){
        return $this->where("email='$email'")->select();
    }

    public function searchArticleByKeyword($Keyword){
        $data['username']  = array('like',"%$Keyword%");
//        return D('Sort')->where($data)->buildSql();
        return D('Sort')->where($data)->select();
    }

    public function checkUsernameExsist($username){
        if($this->getUserByUsername($username))return true;
        return false;
    }

    public function checkEmailExsist($email) {
        if ($this->getUserByEmail($email)) return true;
        return false;
    }


    public function checklogin()
    {
    if($_SESSION['uid']==""){
            return false;
        }
        return true;
    }

    public function checkadmin()
    {
        if($_SESSION['uid']=="") return false;
        $user=$this->getUserByUsername($_SESSION['uid']);
        if($user[0]['user_type']!='admin')return false;
        return true;
    }

}